﻿using System;
using System.Data;
using System.Web.UI;
using System.ComponentModel;

namespace ASPNETVN.PORTAL.Components.Share.Controls
{
    public class PageLink : Control
    {
        [Bindable(true)]        
        [Browsable(true)]
        public string ViewStateKey
        {
            set;
            get;
        }

        public void AddLink(string title, string url)
        {
            DataTable dt = (DataTable)ViewState[this.ViewStateKey];
            if (dt == null)
            {
                dt = new DataTable();
                dt.Columns.Add("Title", typeof(string));
                dt.Columns.Add("Url", typeof(string));
                ViewState[this.ViewStateKey] = dt;
            }
            DataRow dr = dt.NewRow();
            dr["Title"] = title;
            dr["Url"] = url;
            dt.Rows.Add(dr);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (string.IsNullOrEmpty(this.ViewStateKey)) this.ViewStateKey = "NONE";
            DataTable dtLink = (DataTable)ViewState[this.ViewStateKey];
            if (dtLink == null || dtLink.Rows.Count == 0)
                return;

            writer.WriteLine("<p>");

            bool bFirst = true;
            foreach (DataRow row in dtLink.Rows)
            {
                if (!bFirst)
                {
                    writer.WriteLine("<span class='Title'>&#187;</span>");
                }
                else
                {
                    bFirst = false;
                }
                writer.WriteLine(String.Format("<a href='{0}' class='Title'>{1}</a>", row["Url"], row["Title"]));
            }

            writer.WriteLine("</p><hr />");
        }
    }
}
